<?
//------------------------------------------
//------------------------------------------
// Library Functions
//------------------------------------------
//------------------------------------------

//------------------------------------------
function readCurrentDateTime()
//------------------------------------------
{
    $s_day    = date('d');
    $s_month  = date('m');
    $s_year   = date('Y');
    $s_sec    = date('s');
    $s_min    = date('i');
    $s_hour   = date('H');

    $res = $s_year.','.$s_month.','.$s_day.','.$s_hour.','.$s_min.','.$s_sec;
    return($res);
}
//------------------------------------------
function readCurrentValue($file)
//------------------------------------------
{
  if(!file_exists($file))
    {
      return("-");;
    }

  $in = fopen($file,"r");
  if($in)
    {
      $row = fgets($in);
    }  
  fclose($in);
  return($row);
}
//------------------------------------------
function writeNewValue($file,$value)
//------------------------------------------
{
  $out = fopen($file, 'w');
  fwrite($out,$value);
  fclose($out);
}

//------------------------------------------
function subSecondsFromDate($diffSec,$year,$month,$day,$hour,$min,$sec)
//------------------------------------------
{
    
    
    $dateTimePow = new DateTime();
    $dateTimePow->setDate($year, $month, $day);
    $dateTimePow->setTime($hour, $min , $sec);
    
    $dateTimePow->sub(new DateInterval("PT".floor($diffSec)."S"));
    
    
    //translate
    $date_time_arr = Array();
    $date_time_arr['year'] = $dateTimePow->format("Y");
    $date_time_arr['month']= $dateTimePow->format("m");
    $date_time_arr['day']  = $dateTimePow->format("d");
    
    $date_time_arr['hour'] = $dateTimePow->format("H");
    $date_time_arr['min']  = $dateTimePow->format("i");
    $date_time_arr['sec']  = $dateTimePow->format("s");
    
    //Get fraction 
    $frac = fmod($diffSec,1);
    if($frac != 0){
        $date_time_arr['sec'] = (float)$date_time_arr['sec']-$frac;   
    }
    
    return $date_time_arr;
    
}

?>
